package it.unisannio.lesim.dao;

import it.unisannio.lesim.fsm.Message;
import java.sql.SQLException;
import java.util.ArrayList;
import it.unisannio.lesim.util.ConnectionPool;
import it.unisannio.lesim.util.exception.ConnectionPoolException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

/**
 *
 * @author Marco Calisti
 * @author Antonio Cuomo
 * @author Rocco Mazzeo
 * @author Francesco Pacilio
 * 
 */

public class MessageDAO {

    public ArrayList readMessages() throws ConnectionPoolException, SQLException {
        ArrayList messages = new ArrayList();
        ConnectionPool cp = ConnectionPool.getConnectionPool();
        Connection c = cp.getConnection();
        Statement s = c.createStatement();

        Message m;
        try {
            ResultSet rs = s.executeQuery("select no, buff, sec, usec, sourceip,sourceport, desip, desport,worknum, agentnum, protocoltype from sipana.message order by no;");
            while (rs.next()) {
                m = new Message();
                m.setNo(rs.getInt("no"));
                m.setBuff(rs.getString("buff"));
                m.setSec(rs.getInt("sec"));
                m.setUsec(rs.getInt("usec"));
                
                messages.add(m);
            }
        } catch (Exception e) {
            messages = null;
            e.printStackTrace();
        }
        cp.releaseConnection(c);
        
        return messages;
    }

    public ArrayList readConversations() throws ConnectionPoolException, SQLException {
        ArrayList conversations = new ArrayList();
        ConnectionPool cp = ConnectionPool.getConnectionPool();
        Connection c = cp.getConnection();
        Statement s = c.createStatement();
        Vector v = new Vector();
        Message m;
        String call;
        try {
            ResultSet rs = s.executeQuery("select no, buff, sec, usec, sourceip,sourceport, desip, desport,worknum, agentnum, protocoltype from sipana.message order by no;");
            while (rs.next()) {
                m = new Message();
                m.setNo(rs.getInt("no"));
                m.setBuff(rs.getString("buff"));
                m.setSec(rs.getLong("sec"));
                m.setUsec(rs.getLong("usec"));

                call = m.getCallId();
                if (!v.contains(call)) {
                    v.add(call);
                    conversations.add(m);
                }
            }
        } catch (Exception e) {
            conversations = null;
            e.printStackTrace();
        }
        cp.releaseConnection(c);
        
        return conversations;
    }
    
    public ArrayList readMessagesFromConversation(String callId) throws ConnectionPoolException, SQLException {
        ArrayList conversations = new ArrayList();
        ConnectionPool cp = ConnectionPool.getConnectionPool();
        Connection c = cp.getConnection();
        Statement s = c.createStatement();
        Vector v = new Vector();
        Message m;
        String call;
        try {
            ResultSet rs = s.executeQuery("select no, buff, sec, usec, sourceip,sourceport, desip, desport,worknum, agentnum, protocoltype from sipana.message order by no;");
            while (rs.next()) {
                m = new Message();
                m.setNo(rs.getInt("no"));
                m.setBuff(rs.getString("buff"));
                m.setSec(rs.getInt("sec"));
                m.setUsec(rs.getInt("usec"));

                call = m.getCallId();
                if (call.contains(callId)) {
                    v.add(call);
                    conversations.add(m);
                }
            }
        } catch (Exception e) {
            conversations = null;
            e.printStackTrace();
        }
        cp.releaseConnection(c);
        
        return conversations;
    }
    
    public ArrayList readMessagesFromIDs(String[] no) throws ConnectionPoolException, SQLException {
        ArrayList messages = new ArrayList();
        ConnectionPool cp = ConnectionPool.getConnectionPool();
        Connection c = cp.getConnection();
        Statement s = c.createStatement();
        Message m;
        Vector v = new Vector();
            for(int j=0; j<no.length; j++)
                v.add(no[j]);
            try {
            ResultSet rs = s.executeQuery("select no, buff, sec, usec, sourceip,sourceport, desip, desport,worknum, agentnum, protocoltype from sipana.message order by no;");
            while (rs.next()) {
                m = new Message();
                m.setNo(rs.getInt("no"));
                m.setBuff(rs.getString("buff"));
                m.setSec(rs.getInt("sec"));
                m.setUsec(rs.getInt("usec"));

                if (v.contains(m.getNo()+"")) {
                    messages.add(m);
                }
                
            }
        } catch (Exception e) {
            messages = null;
            e.printStackTrace();
        }
        cp.releaseConnection(c);
        
        return messages;
    }
    
}
